package com.ellecity06.cwhiskymall.ui.address.add

import com.ellecity06.common.base.BaseMviViewModel
import com.ellecity06.common.ktx.postEvent
import com.ellecity06.common.utils.ToastUtils
import com.ellecity06.common.utils.logD
import com.ellecity06.cwhiskymall.bean.request.AddressAddRequest
import com.ellecity06.cwhiskymall.bean.request.AddressEditRequest
import com.ellecity06.cwhiskymall.http.request
import com.github.gzuliyujiang.wheelpicker.entity.CityEntity
import com.github.gzuliyujiang.wheelpicker.entity.CountyEntity
import com.github.gzuliyujiang.wheelpicker.entity.ProvinceEntity


/**
 * @author ellecity06
 * @time 2024/10/31 13:47
 * @des
 */
class AddressAddViewModel : BaseMviViewModel<AddressAddState, AddressAddIntent>() {
    // 省份
    private var province: ProvinceEntity? = null

    // 市
    private var city: CityEntity? = null

    // 区县
    private var county: CountyEntity? = null

    override suspend fun collectIntent(it: AddressAddIntent) {
        when (it) {
            is AddressAddIntent.ChooseCity -> {
                province = it.province
                city = it.city
                county = it.county
            }
            is AddressAddIntent.AddAddress -> {

                addressAdd(it)
            }
            is AddressAddIntent.FetchAddress -> {
                if (it.id == -1) return
                fetchAddress(it.id)
            }
            is AddressAddIntent.EditAddress -> {}
        }
    }

    private fun fetchAddress(id: Int) {
        request(api = { fetchAddressById(AddressEditRequest(id)) }, success = {
            if (it != null) {

                province = ProvinceEntity().apply {
                    code = it.provinceCode
                }
                city = CityEntity().apply {
                    code = it.cityCode
                }
                county = CountyEntity().apply { code = it.districtCode }
                mState.postEvent(AddressAddState.FetchAddressSuccess(it))
            }

        })
    }

    private fun addressAdd(it: AddressAddIntent.AddAddress) {
        if (it.name.isEmpty()) {
            ToastUtils.show("请输入收件人姓名")
            return
        }
        if (it.phone.isEmpty()) {
            ToastUtils.show("请输入手机号")
            return
        }
        if (it.city.isEmpty()) {
            ToastUtils.show("请选择省/市/区")
            return
        }
        if (it.address.isEmpty()) {
            ToastUtils.show("请输入详细的收货地址")
            return
        }

        if (it.id == -1) {
            val request = AddressAddRequest(
                memberName = it.name,
                telMobile = it.phone,
                addressAll = it.city,
                detailAddress = it.address,
                provinceCode = province?.code,
                cityCode = city?.code,
                districtCode = county?.code,
                isDefault = if (it.default) 1 else 0,
            )
            request(api = { addressAdd(request) }, showLoading = true, success = {
                mState.postEvent(AddressAddState.AddSuccess)
            })
        } else {
            val request = AddressAddRequest(
                memberName = it.name,
                telMobile = it.phone,
                addressAll = it.city,
                detailAddress = it.address,
                provinceCode = province?.code,
                cityCode = city?.code,
                districtCode = county?.code,
                isDefault = if (it.default) 1 else 0,
                id = it.id
            )
            request(api = { addressEdit(request) }, showLoading = true, success = {
                mState.postEvent(AddressAddState.AddSuccess)
            })
        }

    }
}